﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.IO;
using KnowWare.Model;
using Xceed.Wpf.Toolkit;
using Wpf.Util;
using System.ComponentModel;

namespace KnowWare
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        YEevent aktuelleEvent;
        int aktuelleKategori = 0;
        Hold aktuelleHold;


        public MainWindow()
        {
            InitializeComponent();
            SplashScreen sp = new SplashScreen("/SplashScreen1.jpg");
            sp.Show(false, true);
            System.Threading.Thread.Sleep(3000);
            sp.Close(new TimeSpan(0));
            aktuelleKategori = 0;

        }


        public MainWindow(String filnavn)
        {

        }

        private void btn_evt_nyt_Click(object sender, RoutedEventArgs e)
        {

            aktuelleEvent = new YEevent(tb_evt_navn.Text, DateTime.Now, FindSti());
            aktuelleEvent.KriterieExcelParser();
            aktuelleEvent.HoldExcelParser();
            ListviewFiller(aktuelleEvent.Kategorier);

            foreach (var item in aktuelleEvent.Kategorier)
            {
                cB_kat.Items.Add(item.Navn);
            }
            cB_kat.SelectedIndex = 0;



        }

     

        public void UISkaber()
        {
            stackPanel1.Children.Clear();
            stackPanel2.Children.Clear();
            gB_hold.Width = 0;
            for (int i = 0; i < aktuelleEvent.Kategorier[aktuelleKategori].KriterieNavn.Count; i++)
            {
                Label lb = new Label();
                lb.Name = "lbl_" + i;
                lb.Content = aktuelleEvent.Kategorier[aktuelleKategori].KriterieNavn[i];
                stackPanel1.Children.Add(lb);
                IntegerUpDown iup = new IntegerUpDown();
                iup.Name = "iup_" + i;
                iup.Maximum = aktuelleEvent.Kategorier[aktuelleKategori].KriterieMax[i];
                iup.Minimum = 0;
                iup.Value = 0;
                iup.Height = lb.Height;
                iup.Margin = new Thickness(6, 6, 0, 0);
                iup.Width = 50;
                

                iup.PreviewTextInput += new TextCompositionEventHandler(iup_PreviewTextInput);
                if (gB_hold.Width < lb.Content.ToString().Length * 6 + 50 + 12)
                {
                    gB_hold.Width = lb.Content.ToString().Length * 6 + 50 + 18;

                }

                stackPanel2.Children.Add(iup);
            }



        }

        void iup_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            int temp;
            IntegerUpDown iup = (IntegerUpDown)sender;
            if (!int.TryParse(e.Text, out temp))
            {
                
                e.Handled = true;

            }
            else
            {
                try
                {
                    if (int.Parse(iup.Text) > iup.Maximum)
                    {
                        iup.Text = iup.Text;
                        e.Handled = true;
                    }
                }
                catch (Exception)
                {

                    e.Handled = false;
                }


            }
        }


        public void ListviewFiller(List<Kategori> kListe)
        {
            
            lv_holdListe.Items.Clear();
            foreach (Hold hold in kListe[aktuelleKategori].HListe)
            {
                lv_holdListe.Items.Add(hold);
            
            }
            lv_holdListe.Items.SortDescriptions.Clear();
            lv_holdListe.Items.SortDescriptions.Add(new SortDescription("PointSum", ListSortDirection.Descending));
            lv_holdListe.Items.SortDescriptions.Add(new SortDescription("Stand", ListSortDirection.Ascending));
            
        }

        private void btn_registrer_Click(object sender, RoutedEventArgs e)
        {

            List<int> intListe = new List<int>();
            for (int i = 0; i < aktuelleEvent.Kategorier[aktuelleKategori].KriterieNavn.Count; i++)
            {
                IntegerUpDown iup = (IntegerUpDown)stackPanel2.Children[i];
                intListe.Add(int.Parse(iup.Text));
            }
            aktuelleHold.Summer(intListe);
            ListviewFiller(aktuelleEvent.Kategorier);

        }



        private void btn_evt_fil_Click(object sender, RoutedEventArgs e)
        {

        }
        private string FindSti()
        {
            return Directory.GetCurrentDirectory() + "../../../Book.csv";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void lv_holdListe_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lv_holdListe.SelectedItem == null)
            {
                return;
            }
            aktuelleHold = (Hold)lv_holdListe.SelectedItem;
            aktuelleKategori = aktuelleEvent.FindKategori(aktuelleHold.Kategori);

        }

        private void cB_kat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            aktuelleKategori = cB_kat.SelectedIndex;
            ListviewFiller(aktuelleEvent.Kategorier);
            UISkaber();
        }

        private void tB_søg_TextChanged(object sender, TextChangedEventArgs e)
        {
            string søgeStreng = tB_søg.Text;
            lv_holdListe.Items.Clear();
            foreach (var item in aktuelleEvent.Kategorier[aktuelleKategori].HListe)
            {
                if (item.HoldNavn.ToLower().Contains(søgeStreng.ToLower()))
                {
                    lv_holdListe.Items.Add(item);
                }
            }

        }

    }
}
